
#!/bin/bash
set -e
if [[ $# -ne 3 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    echo ":param srcDir: 软件包存放目录绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
[[ ! "$3" =~ ^/.* || "$3" = "/" ]] &&  echo "请输入正确的软件包存放目录绝对路径" && exit 1
create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)
srcDir=$3
yum install boost-devel libstdc++ zlib zlib-devel bzip2 environment-modules bc -y
source /etc/profile


set -e
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/bisheng-compiler-2.1.0
module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles
bash Rosetta-3.13.sh $buildpath $installpath $srcDir

mkdir -p pkg/Rosetta-3.13-hpc && cp -r $installpath/* pkg/Rosetta-3.13-hpc/
cat > "pkg/Rosetta-3.13-hpc/run.sh" <<EOF
yum install boost-devel libstdc++ zlib zlib-devel bzip2 environment-modules bc -y
pwd_path=\$('pwd')
basepath=\$(cd \$(dirname "\${BASH_SOURCE[0]}") && pwd)
for d in \$(ls \$basepath)
do
    [[ ! -f "\$basepath/\$d" ]] && cd \$basepath/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done
source /etc/profile &> /dev/null
cd \$pwd_path
EOF
cd pkg/ && tar zcvf Rosetta-3.13-hpc.tar.gz Rosetta-3.13-hpc

rm -rf $buildpath $installpath
